package cn.tedu.medicalpath.employee.service.impl;

import cn.tedu.medicalpath.base.exception.ServiceException;
import cn.tedu.medicalpath.base.response.StatusCode;
import cn.tedu.medicalpath.employee.mapper.EmployeeMapper;
import cn.tedu.medicalpath.employee.pojo.dto.EmployeeQuery;
import cn.tedu.medicalpath.employee.pojo.dto.EmployeeSaveParam;
import cn.tedu.medicalpath.employee.pojo.entity.Employee;
import cn.tedu.medicalpath.employee.pojo.vo.EmployeeVO;
import cn.tedu.medicalpath.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List<EmployeeVO> selectEmployee(EmployeeQuery employeeQuery) {
        log.debug("开始处理-【查询员工】业务-入参：{}",employeeQuery);
        List<EmployeeVO> list = employeeMapper.selectEmployee(employeeQuery);
        if(list.isEmpty()){
            throw new ServiceException(StatusCode.FAILED);
        }
        return list;
    }

    @Override
    public void insert(EmployeeSaveParam employeeSaveParam) {
        log.debug("开始处理-【新增员工】业务-入参：{}",employeeSaveParam);
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeSaveParam,employee);
        if(employeeSaveParam.getId() == null){
            employee.setCreateTime(new Date());
            employee.setStatus("0");
            employeeMapper.insert(employee);
        }else {
            employee.setUpdateTime(new Date());
            employeeMapper.update(employee);
        }
    }

    @Override
    public void delete(Integer id) {
        log.debug("开始处理-【删除员工】业务-入参：{}",id);
        employeeMapper.delete(id);
    }
}
